package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.condition.ProConditionHelp;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormDataIdHandlerImpl extends ConditionFormLinkDataHandlerImpl {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.DATAID.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,  FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        return paasFormDataEntity.getId();
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {

        return businessRuleInfoPojo.getObject();
    }


    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        // 所选数据没有固定值，此处不会执行
        return null;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        // 所选数据没有固定值，此处不会执行
        return null;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return handlerFixedValue(thisField, businessRuleInfoPojo, filterCondition);
    }
}
